//
//  SkillSelectViewController.m
//  XJMTechnician
//
//  Created by 高波 on 2019/1/9.
//  Copyright © 2019 Mr.hu. All rights reserved.
//

#import "SkillSelectViewController.h"
#import "SkillModel.h"
#import "SkillSelectCell.h"
@interface SkillSelectViewController () <UITableViewDelegate, UITableViewDataSource>
{
    SkillModel *_model;
    NSMutableArray *_skills;
    NSMutableArray *_selectSkills;
    NSString *_skillStr;
    NSString *_skillIds;
    UIButton *_rightButton;
}

@property (weak, nonatomic) IBOutlet UITableView *tableView;


@end

@implementation SkillSelectViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"选择技能";
    [self setRightNavigationItem];
    [self getData];

}

- (void)setRightNavigationItem
{
    _rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _rightButton.frame = CGRectMake(0, 0, 60, 40);
    [_rightButton setTitle:@"确定" forState:UIControlStateNormal];
    [_rightButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    _rightButton.titleLabel.font = [UIFont systemFontOfSize:14];
    [_rightButton addTarget:self action:@selector(sure) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:_rightButton];
}

- (void)sure
{
    if (_selectSkills.count == 0) {
        [XJToolClass tipAnimationWith:@"必须选择一项技能"];
        return;
    }
    
    [XJHttpClass FixSkillToken:[XJToolClass getToken] skill:_skillIds andFinishblock:^(id response, NSString *errStr) {
        if (errStr) {
            [XJToolClass tipAnimationWith:errStr];
        }else{
            if ([_delegate respondsToSelector:@selector(SkillSelectViewController:skills:)]) {
                [_delegate SkillSelectViewController:self skills:_skillStr];
            }
            [self.navigationController popViewControllerAnimated:YES];
            [XJToolClass tipAnimationWith:@"选择成功！"];
        }
    }];
}

- (void)getData
{
    _skills = [NSMutableArray array];
    _selectSkills = [NSMutableArray array];
    [XJHttpClass TeachSkillListFinishblock:^(id response, NSString *errStr) {
        if (errStr) {
            [XJToolClass tipAnimationWith:errStr];
        }else{
            [SkillModel mj_setupObjectClassInArray:^NSDictionary *{
                return @{
                         @"data" : @"SkillInfoModel"
                         };
            }];
            _model = [SkillModel mj_objectWithKeyValues:response];
            [_skills addObjectsFromArray:_model.data];
            
            NSMutableArray *skills = [NSMutableArray array];
            NSMutableArray *skillIds = [NSMutableArray array];
            for (SkillInfoModel *model in _skills) {
                if ([model.trues integerValue] == 1) {
                    [skills addObject:model.name];
                    [skillIds addObject:model.cate_id];
                    [_selectSkills addObject:model];
                    
                }
            }
            _skillStr = [skills componentsJoinedByString:@","];
            _skillIds = [skillIds componentsJoinedByString:@","];
            
            [self.tableView reloadData];
        }
    }];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _skills.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"SkillSelectCell";
    SkillSelectCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    if (cell == nil) {
        cell = [SkillSelectCell cellFromNib];
    }
    
    cell.model = _skills[indexPath.row];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 60;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    SkillInfoModel *model = _skills[indexPath.row];
    
    SkillInfoModel *newModel = model;
    if ([model.trues integerValue] == 0) {
        newModel.trues = @"1";
    }else {
        newModel.trues = @"0";
    }
    
    [_skills removeObjectAtIndex:indexPath.row];
    [_skills insertObject:newModel atIndex:indexPath.row];
    
    _selectSkills = [NSMutableArray array];
    NSMutableArray *skills = [NSMutableArray array];
    NSMutableArray *skillIds = [NSMutableArray array];
    for (SkillInfoModel *model in _skills) {
        if ([model.trues integerValue] == 1) {
            [skills addObject:model.name];
            [skillIds addObject:model.cate_id];
            [_selectSkills addObject:model];
        }
    }
    _skillStr = [skills componentsJoinedByString:@","];
    _skillIds = [skillIds componentsJoinedByString:@","];
    
    [self.tableView reloadData];
}

@end
